#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<sys/time.h>
#include<signal.h>

using namespace std;
struct itimerval val_alarm;

void sig_handler(int sig)
{
    cout<<"timer has expired,elapsed "<<val_alarm.it_value.tv_sec<<" seconds"<<endl;
    val_alarm.it_value.tv_sec++;
    if(setitimer(ITIMER_REAL,&val_alarm,NULL)==-1)
        perror("failed to call setitimer");
}
int main()
{
    val_alarm.it_interval.tv_sec=0;
    val_alarm.it_interval.tv_usec=0;
    val_alarm.it_value.tv_sec=1;
    val_alarm.it_value.tv_usec=0;
    struct sigaction sa;
    memset(&sa,0,sizeof sa);
    sa.sa_handler=&sig_handler;
    if((sigaction(SIGALRM,&sa,NULL))==-1)
        perror("failed to call sigaction");
    if((setitimer(ITIMER_REAL,&val_alarm,NULL))==-1)
        perror("failed to call setitimer");
    for(;;);
    return 0;
}